#!/bin/bash
#********************************************************************
# Author: zhangjunjie
# Date: 2022-09-05
# FileName： ubuntu_install_docker.sh
#********************************************************************

## 判断自定义方法
if [ ! -e self_function.sh ]; then
	dpkg -s wget || (apt install wget -y || { echo -e "\E[31;1m 安装 wget 软件包失败 \E[0m" ; exit; })

	echo -e "\E[33;1m 下载自定义方法中 \E[0m"
	wget https://gitee.com/gkdaxue/shell_script/raw/master/self_function.sh
	if [ ! -e self_function.sh ]; then 
		echo -e "\E[31;1m 下载自定义方法失败 \E[0m"
		exit;
	fi
fi

## 加载自定义方法
. ./self_function.sh
actions 'echo_tips' "加载自定义方法";


DOCKER_VERSION="5:19.03.15~3-0~ubuntu-bionic"
DOCKER_COMPOSE_VERSION="v2.5.0"
DOCKER_COMPOSE_NAME="docker-compose-`uname -s`-`uname -m`"
HARBOR_VERSION="1.7.6"
HARBOR_ADMIN_PASSWORD=123456
IPADDR=`hostname -I|awk '{print $1}'`

## 安装docker
install_docker (){
	actions 'echo_tips' "开始安装 Docker";

	dpkg -s docker-ce &> /dev/null 
	if [ $? -ne 0 ]; then
		apt update
		rm -rf /var/lib/dpkg/lock /var/lib/dpkg/lock-frontend
		apt -y install apt-transport-https ca-certificates curl software-properties-common
		curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
		add-apt-repository "deb [arch=amd64] https://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
		apt update

		actions 'echo_tips' "Docker有以下版本";
		apt-cache madison docker-ce
		actions 'echo_tips' "5秒后即将安装: docker "${DOCKER_VERSION}" 版本.....";
		actions 'echo_tips' "如果想安装其它Docker版本，请按ctrl+c键退出，修改版本再执行";

		sleep 5
		apt -y install docker-ce=${DOCKER_VERSION} docker-ce-cli=${DOCKER_VERSION}
		mkdir -p /etc/docker
		cat > /etc/docker/daemon.json <<-'EOF'
{
	"registry-mirrors": ["https://si7y70hh.mirror.aliyuncs.com"] 
}
EOF
		systemctl daemon-reload
		systemctl enable --now docker
		docker version && { actions 'echo_success' "Docker安装成功"; } || { actions 'echo_error' "Docker安装失败"; }
	fi		
}

## 安装 docker compose
install_docker_compose () {
	install_docker

	docker-compose --version &> /dev/null

	if [ $? -ne 0 ]; then
		actions 'echo_tips' "开始安装 Docker compose....."; 
		sleep 1 
		# wget https://github.com/docker/compose/releases/download/${DOCKER_COMPOSE_VERSION}/${DOCKER_COMPOSE_NAME}
		wget https://get.daocloud.io/docker/compose/releases/download/${DOCKER_COMPOSE_VERSION}/${DOCKER_COMPOSE_NAME}
		mv ${DOCKER_COMPOSE_NAME} /usr/bin/docker-compose
		chmod +x /usr/bin/docker-compose
		docker-compose --version && { actions 'echo_success' "Docker Compose 安装完成"; } || { actions 'echo_error' "Docker compose 安装失败"; }
	fi	
}

## 安装 harbor
install_harbor () {
	install_docker_compose
	apt -y install python

	actions 'echo_tips' "开始安装 Harbor ....."; 
	wget https://storage.googleapis.com/harbor-releases/release-1.7.0/harbor-offline-installer-v${HARBOR_VERSION}.tgz
	mkdir /apps
	tar xvf harbor-offline-installer-v${HARBOR_VERSION}.tgz -C /apps
	sed -i.bak -e 's/^hostname =.*/hostname = '''$IPADDR'''/' -e 's/^harbor_admin_password =.*/harbor_admin_password = 	'''$HARBOR_ADMIN_PASSWORD'''/' /apps/harbor/harbor.cfg
	/apps/harbor/install.sh && { actions 'echo_success' "Harbor 安装完成"; } || { actions 'echo_error' "Harbor 安装失败"; }

	cat > /lib/systemd/system/harbor.service <<EOF
[Unit]
Description=Harbor
After=docker.service systemd-networkd.service systemd-resolved.service
Requires=docker.service
Documentation=http://github.com/vmware/harbor

[Service]
Type=simple
Restart=on-failure
RestartSec=5
ExecStart=/usr/bin/docker-compose -f /apps/harbor/docker-compose.yml up
ExecStop=/usr/bin/docker-compose -f /apps/harbor/docker-compose.yml down

[Install]	
WantedBy=multi-user.target
EOF

	systemctl daemon-reload 
	systemctl enable harbor &>/dev/null || { actions 'echo_success' "Harbor已配置为开机自动启动"; }
}

install_menu=(
"ubuntu_1804_yum_docker1903"
"ubuntu_1804_github_docker_composev2.5.0+yum_docker1903"
"ubuntu_1804_harbor"
)

PS3="请选择你要如何安装 : "
echo "我们提供如下菜单: "
select select_menu in ${install_menu[@]} ; do
	case $select_menu in
		## ubuntu 1804 yum源安装 docker 1903 中最新的版本
		"ubuntu_1804_yum_docker1903")
			install_docker
			exit;
			;;
		## ubuntu 1804 yum源安装 docker compose + docker 1903 中最新的版本
		"ubuntu_1804_github_docker_composev2.5.0+yum_docker1903")
			install_docker_compose
			exit;
			;;
		## ubuntu 1804 安装 harbor + docker 1903 中最新的版本
		"ubuntu_1804_harbor")
			install_harbor
			exit;
			;;			
		*)
			echo "Selected number: $REPLY"
			echo "Selected number: $select_menu"
			exit;
			;;
	esac
done